package cn.zhaopin.starter.security.handler;

import cn.zhaopin.starter.security.cache.SecurityCacheUtil;
import cn.zhaopin.starter.security.common.SecurityConstant;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.server.WebFilterExchange;
import org.springframework.security.web.server.authentication.logout.ServerLogoutSuccessHandler;
import reactor.core.publisher.Mono;

import java.nio.charset.StandardCharsets;

/**
 * description:自定义登出
 *
 * @author: zuomin (myleszelic@outlook.com)
 * @date 2021/10/18:21:05
 */
public class BasicServerLogoutSuccessHandler implements ServerLogoutSuccessHandler {

    private SecurityCacheUtil securityCacheUtil;

    public BasicServerLogoutSuccessHandler(SecurityCacheUtil securityCacheUtil) {
        this.securityCacheUtil = securityCacheUtil;
    }

    @Override
    public Mono<Void> onLogoutSuccess(WebFilterExchange exchange, Authentication authentication) {
        ServerHttpResponse response = exchange.getExchange().getResponse();
        HttpHeaders headers = response.getHeaders();
        headers.set("Content-Type", MediaType.APPLICATION_JSON_VALUE);
        headers.set("Cache-Control", "no-store,no-cache,must-revalidate,max-age-8");

        return response.writeWith(Mono.just(response.bufferFactory().wrap(SecurityConstant.LOGOUT_SUCCESS.getBytes(StandardCharsets.UTF_8))));
    }
}
